package net.siit.ling;

import java.util.ArrayList;
import java.util.List;
import net.siit.datatype.Range;
import org.antlr.runtime.ANTLRStringStream;
import org.antlr.runtime.CharStream;
import org.antlr.runtime.CommonTokenStream;
import org.antlr.runtime.RecognitionException;
import org.antlr.runtime.TokenStream;
import org.antlr.runtime.tree.CommonTree;
import org.antlr.runtime.tree.Tree;

/**
 *
 * @author arthit
 */
public class TCCSeg {
    public List<Range> clusterAsRange(String text) {
        ANTLRStringStream stream = new ANTLRStringStream(text);
        if (stream == null) {
            return null;
        }
        return clusterAsRange(stream);
    }

    public List<Range> clusterAsRange(CharStream stream) {
        TCCLexer lexer   = new TCCLexer(stream);
        TokenStream tokens = new CommonTokenStream(lexer);
        TCCParser parser = new TCCParser(tokens);
        TCCParser.s_return r = null;
         
        try {
            r = parser.s();
        } catch(RecognitionException e) {
            e.printStackTrace();
            return null;
        }

        List<Range> clusters
                = new ArrayList<Range>();
        
        int index = 0;
        if (r != null) {
            CommonTree t = (CommonTree) r.getTree();
            if (t != null) {
                int tokenNum = t.getChildCount();
                for (int i = 0; i < tokenNum; i++) {
                    Tree token = t.getChild(i);
                    int type = token.getType();
                    if (type == TCCParser.TCC | type == TCCParser.BTCC | type == TCCParser.ATCC) {
                        Range cluster = new Range();
                        int end = index + token.getChildCount();
                        cluster.setStart(index);
                        cluster.setEnd(end);
                        clusters.add(cluster);
                        index = end;
                    }
                }
            }
        }
        return clusters;
    }

    public List<String> clusterAsString(CharStream stream) {
        TCCLexer lexer   = new TCCLexer(stream);
        TokenStream tokens = new CommonTokenStream(lexer);
        TCCParser parser = new TCCParser(tokens);
        TCCParser.s_return r = null;
 
        try {
            r = parser.s();
        } catch(RecognitionException e) {
            e.printStackTrace();
            return null;
        }

        List<String> clusters
                = new ArrayList<String>();
        
        int index = 0;
        if (r != null) {
            CommonTree t = (CommonTree) r.getTree();
            if (t != null) {
                int tokenNum = t.getChildCount();
                for (int i = 0; i < tokenNum; i++) {
                    Tree token = t.getChild(i);
                    if (token.getType() == TCCParser.TCC) {
                        int end = index + token.getChildCount();
                        String str = stream.substring(index, end - 1);
                        index = end;
                        clusters.add(str);
                    }
                }
            }
        }
        return clusters;
    }

}
